/*
 * @Author: jiaxiuc
 * @Date: 2024-12-30 09:58:12
 * @LastEditors: jiaxiuc
 * @LastEditTime: 2025-02-11 16:07:30
 */
function saveLocal(key, value) {
  chrome.storage.local.set({ [key]: value });
}
async function getLocal(key) {
  const result = await chrome.storage.local.get([key]);
  return result[key];
}
window.addEventListener(
  'message',
  (e) => {
    if (!e.data || !e.data.type) return false;
    switch (e.data.type) {
      case 'user': //登录同步
        saveLocal('userVal', JSON.stringify(e.data.data));
        chrome.runtime.sendMessage(e.data, (response) => {
          // sendResponse({ type: "success" });
        });
        break;
      case 'talent': //关联同事查询
        const nameVal = e.data.data.names.join(',');
        //console.log('nameVal:',nameVal)
        saveLocal('nameVal', nameVal);
        const companyVal = e.data.data.company;
        const companyName = companyVal.replace('股份有限公司', '').replace('有限公司', '');
        console.log('companyName:',companyName)
        saveLocal('companyVal', companyName);
        if(e.data.data?._id){
          saveLocal('idVal', e.data.data._id);
        }else{
          saveLocal('idVal', '');
        }
        saveLocal('searchReset', true);
        break;
      case 'picture': //登录画像
        //console.log(22222333,e.data?.data,chrome.storage.local)
        saveLocal('pictureVal', e.data?.data);
        break;
      default:
        //console.log(e.data);
        break;
    }
  },
  false
);
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
  if (request.status == 'resultupdate') {
    //console.log('收到message：',request)
    window.postMessage(request, '*');
  }
});
